<?php

namespace app\model;

use think\Model;

class Member extends Model
{

	protected $connection = 'mysql';

	protected $pk = 'id';

	protected $name = 'member';


	function setmemberinfo($member)
	{
		if (!empty($member)) {
			$member['agent_levelname'] = AgentLevel::getLevel($member['agent_level']);
			$member['gname'] = MemberAuthGroup::getGroup($member['gid'])['title'];
			$member['pname'] = Member::getpidname($member['id']);
		}
		return $member;
	}
	public static function getpcarray($ptype = 'store')
	{
		if ($ptype == 'store') {
			$uids = Store::where(['status' => 1, 'weid' => weid()])->column('uid');
		} else if ($ptype == 'technical') {
			$uids = Technical::where(['status' => 1, 'weid' => weid()])->column('uid');
		}
		$data = self::field('id,nickname')->where(['weid' => weid()])->where('id', 'not in', $uids)->select()->toArray();
		$datalist = [];
		foreach ($data as $key => $vo) {
			$datalist[$key]['val'] = $vo['id'];
			$datalist[$key]['key'] = $vo['id'] . ' - ' . $vo['nickname'];
		}
		array_unshift($datalist, ['val' => 0, 'key' => '无']);
		return $datalist;
	}
	function getUserbyopenid()
	{

		if (!empty(getFans()['openid'])) {

			if (empty($userinfo)) {
				$uid = Openid::getuidbyopenid();
				if (!empty($uid)) {
					$userinfo = $this::where(['id' => $uid, 'weid' => weid()])
						->order('id desc')
						->find();
				}
			}

			if (empty($userinfo)) {
				$userinfo = $this::where(['wechat_openid' => getFans()['openid'], 'weid' => weid()])
					->order('id desc')
					->find();

				if (!empty($userinfo)) {
					$odate['weid'] = weid();
					$odate['uid'] = $userinfo->id;
					$odate['uuid'] = $userinfo->uuid;
					$odate['openid'] = getFans()['openid'];
					$odate['ptype'] = getFans()['ptype'];
					Openid::create($odate);
				}
			}

			if (!empty($userinfo)) {
				$userinfo = $userinfo->toArray();
			}
		}
		if (!empty(getFans()['openid']) && !empty($userinfo['id'])) {
			$delcondition = "wechat_openid='" . getFans()['openid'] . "' and weid= " . weid() . " and id<" . $userinfo['id'] . "";
			Member::destroy($delcondition);
		}

		unset($userinfo["password"]);
		return $userinfo;
	}

	function getUserByWechat()
	{

		$userinfo = $this->getUserbyopenid();

		if (!empty($userinfo["id"])) {
			if (empty($userinfo['primaryuid'])) {
				//$this->updatamember($userinfo);
			}
		} else {
			$this->wechatReg();
			$userinfo = $this->getUserbyopenid();
		}

		return $this->setmemberinfo($userinfo);
	}

	function wechatReg()
	{

		if (!empty(getFans()['openid'])) {

			$userinfo = $this->getUserbyopenid();

			if (empty($userinfo)) {

				$member = $this->getmemberdata();
				$member['category_id'] = 0;

				$Configcommon = Config::getconfig('common');

				if ($Configcommon['reg_check'] == 1) {
					$member['status'] = 0;
				} else {
					$member['status'] = 1;
				}

				$agent = Config::getconfig('agent');
				if ($agent['share_condition'] == 3) {

					$agent_levelarray = AgentLevel::where(['weid' => weid()])->order('id asc')->find();

					if (!empty($agent_levelarray)) {
						$agent_levelarray = $agent_levelarray->toArray();
					}

					$agent_level = $agent_levelarray['id'];
					$member['is_agent'] = 1;
					$member['agent_level'] = $agent_level;
				}

				$res = $this::create($member);
				if (!empty($res)) {
					$odate['weid'] = weid();
					$odate['uid'] = $res->id;
					$odate['uuid'] = $res->uuid;
					$odate['openid'] = getFans()['openid'];
					$odate['ptype'] = getFans()['ptype'];
					Openid::create($odate);
				}
			}
		}
	}

	function updatamember($mem = [])
	{
		if (!empty($mem['wechat_openid'])) {

			if (!empty(getFans()['tag']['nickname'])) {
				$member = $this->getmemberdata();
				unset($member["password"]);
				unset($member["regdate"]);
				//unset($member["lastdate"]);
				unset($member["wechat_openid"]);
				unset($member["gid"]);

				$member['id'] = $mem['id'];

				self::update($member);
			}
		}
	}

	function getmemberdata()
	{
		getFans()['tag']['nickname'] =  removeEmoji(getFans()['tag']['nickname']);
		$gid = Config::getconfig()['default_group_id'];
		if (empty($gid)) {
			$membergroup = MemberAuthGroup::where(['weid' => weid()])
				->order('id asc')
				->find();

			if (!empty($membergroup)) {
				$membergroup = $membergroup->toArray();
			}

			$gid = $membergroup['id'];
		}

		if (empty(getFans()['tag']['sex'])) {
			$member['sex'] = 0;
		} else {
			$member['sex'] = getFans()['tag']['sex'];
		}
		$member['weid'] = weid();
		$member['userpic'] = getFans()['tag']['avatar'];
		$member['reg_type'] = 'mobile';
		$member['gid'] = $gid;
		$member['regdate'] = time();
		$member['lastdate'] = time();
		//var_dump(getFans()['tag']['nickname']);
		if (empty(getFans()['tag']['nickname'])) {
			$member['nickname'] = '匿名用户';
		} else {
			$member['nickname'] = getFans()['tag']['nickname'];
		}

		if (!empty(getFans()['tag']['country'])) {
			$member['country'] = getFans()['tag']['country'];
		}

		if (!empty(getFans()['tag']['province'])) {
			$member['province'] = getFans()['tag']['province'];
		}

		if (!empty(getFans()['tag']['city'])) {
			$member['city'] = getFans()['tag']['city'];
		}

		//$member['resume'] = iserializer($_W);
		return $member;
	}


	public static function get_name($uid)
	{
		$ret = self::find($uid);
		return $ret->nickname;
	}

	public static function get_uidByuuid($uuid)
	{
		$ret = self::where('uuid', $uuid)->find();
		return $ret->id;
	}

	public static function getpidname($uid)
	{
		$member = self::find($uid);

		if ($member->pid) {
			$memberpid = self::find($member->pid);
			return $memberpid->nickname;
		} else {
			return '平台';
		}
	}

	public static function getDepart($pid)
	{
		$uidlist = self::where(['pid' => $pid])->select()->toArray();

		foreach ($uidlist as $vo) {
			if (empty($uidliststr)) {
				$uidliststr = $vo['id'];
			} else {
				$uidliststr .= ',' . $vo['id'];
			}
		}
		return explode(',', $uidliststr);
	}

	public static function getonelevel($uid, $isdata = false)
	{
		if ($isdata) {
			return self::field('id,nickname,regdate,userpic')->where(['weid' => weid(), 'pid' => $uid])->select()->toArray();
		} else {
			return self::where(['weid' => weid(), 'pid' => $uid])->count();
		}
		return self::where(['weid' => weid(), 'pid' => $uid])->count();
	}

	public static function gettwolevel($uid, $isdata = false)
	{
		$data['first'] = self::field('id,nickname')->where(['weid' => weid(), 'pid' => $uid])->select()->toArray();
		if (!empty($data['first'])) {
			foreach ($data['first'] as $vo) {
				if (empty($pid)) {
					$pid = $vo['id'];
				} else {
					$pid = $pid . ',' . $vo['id'];
				}
			}
			$pidin = explode(',', $pid);
			if ($isdata) {
				return self::field('id,nickname,regdate,userpic')->where(['weid' => weid(), 'pid' => $pidin])->select()->toArray();
			} else {
				return self::where(['weid' => weid(), 'pid' => $pidin])->count();
			}
		} else {
			return 0;
		}
	}

	public static function getthreelevel($uid, $isdata = false)
	{
		$data['first'] = self::field('id,nickname')->where(['weid' => weid(), 'pid' => $uid])->select()->toArray();
		if (!empty($data['first'])) {
			foreach ($data['first'] as $vo) {
				if (empty($pid)) {
					$pid = $vo['id'];
				} else {
					$pid = $pid . ',' . $vo['id'];
				}
			}
			$pidin = explode(',', $pid);
			$data['first'] = self::field('id,nickname')->where(['weid' => weid(), 'pid' => $pidin])->select()->toArray();

			if (!empty($data['second'])) {
				foreach ($data['second'] as $so) {
					if (empty($tpid)) {
						$tpid = $so['id'];
					} else {
						$tpid = $tpid . ',' . $so['id'];
					}
				}

				$tpidin = explode(',', $tpid);
				if ($isdata) {
					return self::field('id,nickname,regdate,userpic')->where(['weid' => weid(), 'pid' => $tpidin])->select()->toArray();
				} else {
					return self::where(['weid' => weid(), 'pid' => $tpidin])->count();
				}
			} else {
				return 0;
			}
		} else {
			return 0;
		}
	}

	//判断手机号是否已注册
	public function is_register($telephone)
	{
		$is_register = self::where(['telephone' => $telephone])->find();
		if (!empty($is_register)) {
			$is_register = $is_register->toArray();
		}

		if (empty($is_register)) {
			return true;
		} else
			return false;
	}
	//检查是否审核通过!
	public function is_check($uid)
	{
		$result = self::where(['status' => 1, 'id' => $uid])->find();
		if (!empty($result)) {
			return true;
		} else
			return false;
	}
	public function checked($telphone)
	{
		$result = self::where(['telephone' => $telphone])->find();
		if (!empty($result)) {
			return true;
		} else
			return false;
	}
	public function checkAgent($uid)
	{
		$result = self::where(['id' => $uid, 'is_agent' => 1])->find();
		if (empty($result)) {
			exit(\json_encode('错误，请先成为分销商!'));
		}
	}
}
